// DEATH ITEM

AddCSLuaFile( "shared.lua" )
AddCSLuaFile( "cl_init.lua" )
include( 'shared.lua' )

ENT.eSound = Sound("ui/scored.wav")

local weaponsTable = {
	"weapon_wb_expgun",
	"weapon_wb_supergun",
	"weapon_wb_firegun",
	"weapon_wb_airgun",
    "weapon_wb_pushgun",
	"weapon_wb_earthgun",
	"weapon_wb_watergun",
	"weapon_wb_minegun",
	"weapon_wb_crazygun",
	"weapon_wb_medicgun",
	"weapon_wb_rubbergun"
}

local SuperweaponsTable = {
	"weapon_wb_lonegun",
	"weapon_wb_knkgun",
	"weapon_wb_biggun",
	"weapon_wb_obligun",
	"weapon_wb_shockgun",
	"weapon_wb_poligun",
}

function PlayerGetSwep( pl )
local rnd = math.random(1,#weaponsTable)
pl:Give( weaponsTable[rnd] )
end

function PlayerGetSuperSwep( pl )
local rnd = math.random(1,#SuperweaponsTable)
pl:Give( SuperweaponsTable[rnd] )
end

function ENT:SpawnFunction( ply, tr )

	if ( !tr.Hit ) then return end
	
	local SpawnPos = tr.HitPos + tr.HitNormal * 16
	
	local ent = ents.Create( "death_item" )
	ent:SetPos( SpawnPos )
	ent:Spawn()
	ent:Activate()
	
	return ent
	
end

function ENT:Initialize()

	self.Entity:SetModel( "models/items/ammopack_large.mdl"  )
	self.Entity:PhysicsInit( SOLID_VPHYSICS )
	self.Entity:SetMoveType( MOVETYPE_FLY )
	self.Entity:SetSolid( SOLID_VPHYSICS )
	//self.Entity:SetColor(math.random(100,255),math.random(120,255),0,255)
	
	local phys = self.Entity:GetPhysicsObject()
	if (phys:IsValid()) then
		phys:Wake()
	end
	
	timer.Create("Tdeath"..tostring(math.Rand(0,15000))..tostring(CurTime()/10),1.2,0, function()
		if ValidEntity(self.Entity) then
			local vPoint = self.Entity:GetPos() + Vector(0,0,1)
			local effectdata = EffectData()
			effectdata:SetOrigin( vPoint )
			effectdata:SetScale( 1 )
			effectdata:SetStart(self.Entity:GetVelocity())
			util.Effect( "death_item_ef", effectdata )--s
			self.Entity:EmitSound( "weapons/knife_swing_crit.wav" )
		end	
	end)
	
	timer.Create("Tdelete_death"..tostring(math.Rand(0,10000))..tostring(CurTime()/10),60,1, function()
		if ValidEntity(self.Entity) then
			local vPoint = self.Entity:GetPos() + Vector(0,0,1)
			local effectdata = EffectData()
			effectdata:SetOrigin( vPoint )
			effectdata:SetScale( 1 )
			effectdata:SetStart(self.Entity:GetVelocity())
			util.Effect( "death_item_ef", effectdata )--s
			self.Entity:Remove()
		end	
	end)
end

function ENT:Think()
	self.Entity:SetPos(self.Entity:GetPos() + Vector(0,0,math.sin(CurTime()*2)*0.8))
	self.Entity:SetAngles(self.Entity:GetAngles() + Angle(0,0.6,0) )
	
	self.Entity:NextThink( CurTime() + 0.001 )
	return true 
end

function ENT:PhysicsCollide( data, physobj )	
//NADA
end

function ENT:OnTakeDamage( dmginfo )

	self.Entity:TakePhysicsDamage( dmginfo )
	
end

function ENT:Touch( entidadeTocada )
	if !ValidEntity(entidadeTocada) then return end
	if !entidadeTocada:IsPlayer() then return end
	
	self.Entity:EmitSound(self.Entity.eSound,200,100)
	
	chance = math.random(100)
	
	if (chance > 70) then
		PlayerGetSuperSwep( entidadeTocada )
	else
		PlayerGetSwep( entidadeTocada )
	end
	
	if ValidEntity(self.Entity) then
		local vPoint = self.Entity:GetPos() + Vector(0,0,1)
		local effectdata = EffectData()
		effectdata:SetOrigin( vPoint )
		effectdata:SetScale( 0.7 )
		effectdata:SetStart(self.Entity:GetVelocity())
		util.Effect( "death_item_ef", effectdata )--s
		self.Entity:Remove()
	end	

end



